package com.zxl.permission.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zxl.permission.dao.UserDao;
import com.zxl.permission.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service("userService")
public class UserService {
    @Resource
    private UserDao userDao;

    public JSONObject list() {
        // 拉取用户列表
        List<User> userList = userDao.getUserList();
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "success");
        res.put("data", userList);
        return res;
    }

    public JSONObject add(User user) {
        // 添加用户
        Timestamp cur_time = new Timestamp(System.currentTimeMillis());
        user.create_time = cur_time;
        user.update_time = cur_time;
        user.password="";

        userDao.addUser(user);
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "success");
        return res;
    }

    public JSONObject alter(User user) {
        // 修改用户信息
        userDao.alterUser(user);
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "success");
        return res;
    }

    public JSONObject remove(String username) {
        // 删除用户
        userDao.removeUserFromUsername(username);
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "success");
        return res;
    }
}
